package org.geektimes.base.util;

/**
 * @author : KangNing Hu
 */
public class TypeUtils {


	/**
	 * 类型转换
	 *
	 * @param value
	 * @param tClass
	 * @param <T>
	 * @return
	 */
	public static <T> T transform(String value, Class<T> tClass) {
		if (value == null) {
			return null;
		}
		Object result = null;
		///TODO 暂时只支持常用8大类型 及其 string
		if (tClass == String.class) {
			result = value;
		} else if (tClass == Integer.class || tClass == int.class) {
			result = Integer.valueOf(value);
		} else if (tClass == Long.class || tClass == long.class) {
			result = Long.valueOf(value);
		} else if (tClass == Short.class || tClass == short.class) {
			result = Short.valueOf(value);
		} else if (tClass == Byte.class || tClass == byte.class) {
			result = Byte.valueOf(value);
		} else if (tClass == Float.class || tClass == float.class) {
			result = Float.valueOf(value);
		} else if (tClass == Double.class || tClass == double.class) {
			result = Double.valueOf(value);
		} else if (tClass == Character.class || tClass == char.class) {
			result = value.toCharArray()[0];
		} else if (tClass == Boolean.class || tClass == boolean.class) {
			result = Boolean.valueOf(value);
		} else {
			throw new IllegalArgumentException("不支持的类型" + tClass.getName());
		}
		return (T) result;
	}



}
